package com.coocaa.ops.api.v1.home.service.impl;

import com.coocaa.ops.api.common.constant.PolicyGroupCategory;
import com.coocaa.ops.api.common.model.HeaderModel;
import com.coocaa.ops.api.common.validator.PolicyValidator;
import com.coocaa.ops.api.v1.content.cache.ITabInfoCache;
import com.coocaa.ops.api.v1.home.biz.IModuleBiz;
import com.coocaa.ops.api.v1.home.cache.IHomepageCache;
import com.coocaa.ops.api.v1.home.cache.IModuleCache;
import com.coocaa.ops.api.v1.home.handler.StoreModeHandler;
import com.coocaa.ops.api.v1.home.model.output.HomeContent;
import com.coocaa.ops.api.v1.home.model.query.HomeQueryModel;
import com.coocaa.ops.api.v1.home.service.IHomeService;
import com.coocaa.ops.api.v1.home.service.internal.DmpHomeAndModuleService;
import com.coocaa.ops.api.v1.home.util.HomeQueryUtil;
import com.coocaa.ops.api.v1.home.util.HomeResultUtil;
import com.coocaa.ops.api.v1.policy.model.PolicyModel;
import com.coocaa.ops.api.v1.policy.service.IPolicyService;
import com.coocaa.ops.common.core.base.util.ResourceStatusValidator;
import com.coocaa.ops.common.core.biz.homepage.entity.HomeModuleContentConfigInfo;
import com.coocaa.ops.common.core.biz.homepage.entity.HomepageInfoEntity;
import com.coocaa.ops.common.core.biz.tab.entity.TabInfoEntity;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author zen
 * @date 2020-06-19 14:37
 * @description
 */
@Service
public class HomeServiceImpl implements IHomeService {

    @Autowired
    private IPolicyService policyService;

    @Autowired
    private IHomepageCache homepageCache;

    @Autowired
    private IModuleBiz moduleBiz;

    @Autowired
    private IModuleCache moduleCache;

    @Autowired
    private ITabInfoCache tabInfoCache;

    @Autowired
    private DmpHomeAndModuleService dmpHomeAndModuleService;

    @Override
    public HomeContent getHomeContent(HeaderModel header) {
        PolicyModel policyModel = policyService.getPolicyByHeader(header);
        if (policyModel == null) {
            return new HomeContent();
        }
        HomeQueryModel homeQueryModel = HomeQueryUtil.getHomeQuery(header, policyModel);
        HomeContent homeContent =  getHomeModules(homeQueryModel);
        homeContent.setPolicyId(policyModel.getId());
        homeContent.setHomepageId(policyModel.getHomepageId());
        return homeContent;
    }

    private HomeContent getHomeModules(HomeQueryModel queryModel) {
        HomepageInfoEntity homepageModuleList = homepageCache.getHomepageAndModuleList(queryModel);
        handleModuleDmp(queryModel, homepageModuleList);
        //处理卖场模式
        handleStoreMode(queryModel, homepageModuleList);
        handleModuleInfoForTestPolicy(queryModel, homepageModuleList);
        handleTabDmp(queryModel,homepageModuleList.getModuleList());
        handleTabInfo(queryModel, homepageModuleList.getModuleList());
        return HomeResultUtil.convertHomeContent(homepageModuleList.getModuleList(), homepageModuleList);

    }

    private void handleStoreMode(HomeQueryModel queryModel, HomepageInfoEntity homepageModuleList) {
        //卖场模式处理
        if (queryModel.isStoreMode()) {
            Integer storeModuleId = StoreModeHandler.MODULE_ID;
            HomeModuleContentConfigInfo module = moduleCache.getStoreModeModule(storeModuleId, queryModel.getPolicyCategory());
            if (homepageModuleList != null && homepageModuleList.getModuleList() != null && module != null) {
                int index = StoreModeHandler.INDEX;
                List<HomeModuleContentConfigInfo> moduleList = homepageModuleList.getModuleList();
                index = index > moduleList.size() ? moduleList.size() : index;
                homepageModuleList.getModuleList().add(index, module);
            }
        }
    }

    private void handleModuleDmp(HomeQueryModel homeQueryModel, HomepageInfoEntity homepageWithModuleList) {
        //return homepageWithModuleList.getModuleList();
        //主页模块dmp处理
        if (Objects.equals(homepageWithModuleList.getHasDmp(),0)) {
            return;
        }
        List<HomeModuleContentConfigInfo> finalModuleList = dmpHomeAndModuleService.handleModuleDmp(homepageWithModuleList.getModuleList(), homeQueryModel);

        homepageWithModuleList.setModuleList(finalModuleList);
    }

    private void handleModuleInfoForTestPolicy(HomeQueryModel queryModel,HomepageInfoEntity homepageInfoEntity) {
        if (PolicyValidator.isFormalPolicy(queryModel.getPolicyCategory())) {
            return;
        }
        List<HomeModuleContentConfigInfo> moduleList = homepageInfoEntity.getModuleList();
        if (CollectionUtils.isNotEmpty(moduleList)) {
            ListIterator<HomeModuleContentConfigInfo> i = moduleList.listIterator();
            while (i.hasNext()) {
                handleEachModuleForTestPolicy(i);
            }
        }
    }

    private void handleEachModuleForTestPolicy(ListIterator<HomeModuleContentConfigInfo> moduleIterator) {
        HomeModuleContentConfigInfo module = moduleIterator.next();
        Integer duplicateVersion = module.getDuplicateVersion();
        if (duplicateVersion !=null &&  !Objects.equals(duplicateVersion,0)) {
            HomeModuleContentConfigInfo duplicateModule = moduleBiz.getModuleByIdAndVersion(module.getId(), duplicateVersion);
            if (duplicateModule != null && ResourceStatusValidator.isToAuditing(duplicateModule.getStatus())) {
                moduleIterator.set(duplicateModule);
            }
        }
    }

    private void handleTabDmp(HomeQueryModel homeQueryModel, List<HomeModuleContentConfigInfo> moduleList) {
        dmpHomeAndModuleService.handleTabDmp(moduleList, homeQueryModel);
    }




    private void handleTabInfo(HomeQueryModel queryModel, List<HomeModuleContentConfigInfo> moduleList) {
        if (CollectionUtils.isNotEmpty(moduleList)) {
            for (HomeModuleContentConfigInfo module : moduleList) {
                if (module.getTopTabId() != null && !Objects.equals(module.getTopTabId(), 0)) {
                    TabInfoEntity topTab = tabInfoCache.getTabInfo(module.getTopTabId(), queryModel.getPolicyCategory());
                    module.setTopTabInfo(topTab);
                }
                if (module.getBottomTabId() != null && !Objects.equals(module.getBottomTabId(), 0)) {
                    TabInfoEntity bottomTab = tabInfoCache.getTabInfo(module.getBottomTabId(), queryModel.getPolicyCategory());
                    module.setBottomTabInfo(bottomTab);
                }
            }
        }
    }

}
